﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PIMS.Utils
{
    public class NetLog
    {
        private static object objLockEx = new object();
        /// <summary>
        /// 创建日志文件 Log
        /// </summary>
        /// <param name="ex">异常类</param>
        public static void CreateLog(Exception ex)
        {
            lock (objLockEx)
            {
                string path = Application.StartupPath + "\\log";
                if (!Directory.Exists(path))
                {
                    //创建日志文件夹
                    Directory.CreateDirectory(path);
                }
                //发生异常每天都创建一个单独的日子文件[*.log],每天的错误信息都在这一个文件里。方便查找
                path += "\\" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + ".log";
                WriteLogInfo(ex, path);

                //只保存7天内的文件
                DirectoryInfo dyInfo = new DirectoryInfo(Application.StartupPath + "\\log");
                //获取文件夹下所有的文件
                foreach (FileInfo feInfo in dyInfo.GetFiles())
                {
                    //判断文件日期是否小于今天，是则删除
                    if (feInfo.CreationTime < DateTime.Today.AddDays(-6))
                        feInfo.Delete();
                }
            }
        }
        /// <summary>
        /// 写日志信息
        /// </summary>
        /// <param name="ex">异常类</param>
        /// <param name="path">日志文件存放路径</param>
        private static void WriteLogInfo(Exception ex, string path)
        {
            using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
            {
                try
                {
                    sw.WriteLine("*****************************************【"
                    + DateTime.Now.ToLongTimeString()
                    + "】*****************************************");
                    if (ex != null)
                    {
                        sw.WriteLine("【ErrorType】" + ex.GetType());
                        sw.WriteLine("【TargetSite】" + ex.TargetSite); //触发方法
                        sw.WriteLine("【Message】" + ex.Message);
                        sw.WriteLine("【Source】" + ex.Source); //异常对象
                        sw.WriteLine("【StackTrace】" + ex.StackTrace); //调用堆栈
                        sw.Close();
                    }
                    else
                    {
                        sw.WriteLine("Exception is NULL");
                    }
                    sw.WriteLine();
                }
                catch
                {
                }

            }
        }

        private static object objLock = new object();
        /// <summary>
        /// 创建设备日志文件 DeviceStatus
        /// </summary>
        /// <param name="ex">设备状态信息</param>
        public static void CreateLog(string strInfo)
        {
            lock (objLock)
            {
                string path = Application.StartupPath + "\\DeviceStatus";
                if (!Directory.Exists(path))
                {
                    //创建日志文件夹
                    Directory.CreateDirectory(path);
                }
                //每天都创建一个单独的日志文件[*.log],记录信息发送状态
                path += "\\" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + ".log";
                WriteLogInfo(strInfo, path);

                //只保存7天内的文件
                DirectoryInfo dyInfo = new DirectoryInfo(Application.StartupPath + "\\DeviceStatus");
                //获取文件夹下所有的文件
                foreach (FileInfo feInfo in dyInfo.GetFiles())
                {
                    //判断文件日期是否小于今天，是则删除
                    if (feInfo.CreationTime < DateTime.Today.AddDays(-6))
                        feInfo.Delete();
                }

            }
        }
        /// <summary>
        /// 保存设备状态信息
        /// </summary>
        /// <param name="strInfo">设备状态信息</param>
        /// <param name="path">日志文件存放路径</param>
        private static void WriteLogInfo(string strInfo, string path)
        {
            using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
            {
                try
                {
                    sw.WriteLine(DateTime.Now.ToLongTimeString() + "   " + strInfo);
                    //sw.WriteLine(strInfo);
                    sw.Close();
                }
                catch
                { }
            }
        }
    }
}
